home *** CD-ROM | disk | FTP | other *** search
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <net/if.h>
- #include <sys/sockio.h>
- #include <errno.h>
- #include <string.h>
-
- char *
- getdefaultif(char *sbuf)
- {
- char *interface = "le0";
- struct ifconf ifc;
- char buf[128];
- int sock;
-
- if((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
- perror("socket");
- else {
- ifc.ifc_len = sizeof(buf);
- ifc.ifc_buf = buf;
-
- if(ioctl(sock, SIOCGIFCONF, (char *)&ifc) == -1)
- perror("ioctl");
- else
- interface = ifc.ifc_req->ifr_name;
- close(sock);
- }
- if(sbuf){
- strcpy(sbuf, interface);
- return sbuf;
- }
- else {
- return strdup(interface);
- }
- }
-
- #ifdef TEST_VERSION
-
- main()
- {
- printf("%s\n", getdefaultif((char *)0));
- }
-
- #endif
-